function [fields] = chrt_absorb(fields,layer)

lambda = layer.lambda;
mu = layer.mu;
Zp = layer.Zp;
Zs = layer.Zs;
% no_of_lyr=1 means top
% no_of_lyr=n means bottom
no_of_lyr = layer.number;
nlyr = layer.total_lyr;

vx = fields.vx;
vy = fields.vy;
sxx= fields.sxx;
sxy= fields.sxy;
syy= fields.syy;

% vx(3,:) = (vx(3,:) + sxx(3,:)/Zp)/2;
% vy(3,:) = (vy(3,:) + sxy(3,:)/Zs)/2;
% old_sxx = sxx(3,:);
% sxx(3,:)= (sxx(3,:) + Zp*vx(3,:))/2;
% sxy(3,:)= (sxy(3,:) + Zs*vy(3,:))/2;
% % syy(3,:)= syy(3,:) - ((lambda)/(lambda + 2*mu))*(sxx(3,:) + old_sxx);
% syy(3,:)= syy(3,:) - ((lambda)/(lambda + 2*mu))*(sxx(3,:) - old_sxx);

vx(3,:) = (vx(3,:) + sxx(3,:)/Zp)/2;
vy(3,:) = (vy(3,:) + sxy(3,:)/Zs)/2;
old_sxx = sxx(3,:);
sxx(3,:)= (sxx(3,:) + Zp*vx(3,:))/2;
sxy(3,:)= (sxy(3,:) + Zs*vy(3,:))/2;
syy(3,:)= syy(3,:) + ((lambda)/(lambda + 2*mu))*(sxx(3,:) - old_sxx);

vx(end-2,:) = (vx(end-2,:) - sxx(end-2,:)/Zp)/2;
vy(end-2,:) = (vy(end-2,:) - sxy(end-2,:)/Zs)/2;
old_sxx = sxx(end-2,:);
sxx(end-2,:)= (sxx(end-2,:) - Zp*vx(end-2,:))/2;
sxy(end-2,:)= (sxy(end-2,:) - Zs*vy(end-2,:))/2;
syy(end-2,:)= syy(end-2,:) + ((lambda)/(lambda + 2*mu))*(sxx(end-2,:) - old_sxx);

if no_of_lyr == nlyr
    vy(:,end-2) = (vy(:,end-2) - syy(:,end-2)/Zp)/2;
    vx(:,end-2) = (vx(:,end-2) - sxy(:,end-2)/Zs)/2;
    old_syy = syy(:,end-2);
    syy(:,end-2)= (syy(:,end-2) - Zp*vy(:,end-2))/2;
    sxy(:,end-2)= (sxy(:,end-2) - Zs*vx(:,end-2))/2;
    sxx(:,end-2)= sxx(:,end-2) + ((lambda)/(lambda + 2*mu))*(syy(:,end-2)-old_syy);
end

fields.vx = vx;
fields.vy = vy;
fields.sxx= sxx;
fields.sxy= sxy;
fields.syy= syy;

end